package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.EventBean;
import beans.MessageBean;
import beans.UserBean;

import model.EventDAO;
import model.MessageDAO;
import model.UserDAO;

/**
 * Servlet implementation class VisitServlet
 */
@WebServlet("/VisitServlet")
public class VisitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VisitServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{	    

			UserBean user = new UserBean();
			ArrayList<MessageBean> uvm = new ArrayList<MessageBean>();
			user.setEmail(request.getParameter("visitid"));
			String visitid = request.getParameter("visitid");
			HttpSession session = request.getSession();
			UserBean currentUser = (UserBean)session.getAttribute("currentSessionUser");
			String userID = currentUser.getEmail();

			System.out.println(user.getEmail());

			user = UserDAO.visit(user);
			uvm = MessageDAO.recentMessages(user, false);
			ArrayList<EventBean> usersevent = EventDAO.getUsersEvents(user);
			System.out.println(usersevent.size());
			int hostedCount = 0;
			int attendedCount = 0;
			int hostedRatedCount = 0;
			int attendedRatedCount = 0;
			double hostedSum = 0;
			double attendedSum = 0;
			double thisEventGuestSum = 0;
			int thisEventGuestCount = 0;
			for (EventBean event : usersevent) {
				if (event.isFinished()) {
					EventBean eventduplo = EventDAO.getEventsRatingsForStats(user, event);
					if (eventduplo.getHost().equals(user.getEmail())) {
						hostedCount++;
						for (UserBean guest : eventduplo.getGuestList()) {
							if (guest.getGuestToHostRating() != 0) {
								thisEventGuestSum += guest.getGuestToHostRating();
								thisEventGuestCount++;
							}
						}
						if (thisEventGuestCount != 0) {
							hostedRatedCount++;
							hostedSum += (thisEventGuestSum/thisEventGuestCount);
						}
					} else {
						attendedCount++;
						for (UserBean guest : eventduplo.getGuestList()) {
							if (guest.getEmail().equals(user.getEmail())) 
								if (guest.getHostToGuestRating() != 0) {
									attendedRatedCount++;
									attendedSum += guest.getHostToGuestRating();
								}
						}
					}
				}
			}
			user.setTotalAttended(attendedCount);
			user.setTotalHosted(hostedCount);
			if (hostedRatedCount != 0)
				user.setHostRatingAvg(hostedSum/hostedRatedCount);
			else
				user.setHostRatingAvg(0.00);
			if (attendedRatedCount != 0)
				user.setGuestRatingAvg(attendedSum/attendedRatedCount);
			else
				user.setGuestRatingAvg(0.00);


			if (user.isValid())
			{	        
				if (visitid.equals(userID)) {
					response.sendRedirect("LoginServlet?un=" + userID + "&pw=" + currentUser.getPassword());
				} else {
					session.setAttribute("usersvisitingevent", usersevent);
					session.setAttribute("recentvisitingmessages", uvm);
					session.setAttribute("visitingUser", user); 
					response.sendRedirect("profile.jsp"); //logged-in page
				}
			}

			else {
				session.setAttribute("failedLogin", true);
				response.sendRedirect("oops.jsp"); //loopback to login page to display error message
			}
		} 


		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}
	}




	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
